package com.library.facerecognition.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xh.facerecognition.sdk.FaceRecognition;
import com.xh.facerecognition.sdk.FaceRecognitionConfig;


public class DemoActivity extends AppCompatActivity {

    private TextView mResultTv;
    private EditText mUrlEt;
    private CheckBox mFullUrlCb;
    private CheckBox mLiveCb;
    private Button mStartBtn;

    @Override
    protected void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        mResultTv = findViewById(R.id.tv_result);
        mUrlEt = findViewById(R.id.et_url);
        mFullUrlCb = findViewById(R.id.cb_fullurl);
        mLiveCb = findViewById(R.id.cb_live);
        mStartBtn = findViewById(R.id.btn_start);

        mStartBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mResultTv.setText("");

                FaceRecognition face = FaceRecognition.getInstance();
                FaceRecognitionConfig config = new FaceRecognitionConfig.Builder()
                        .setUrl(mUrlEt.getText().toString()) //必填
                        .setEnableFullUrl(mFullUrlCb.isChecked()) //可选,Url是否完整接口地址,默认false
                        .setEnableLive(mLiveCb.isChecked()) //可选,启用活体检测,默认true
                        .build();
                face.setConfig(config);
                face.start(DemoActivity.this, new FaceRecognition.Callback() {
                    @Override
                    public void onSuccess(String idcard) {
                        //人脸识别成功,返回身份证号
                        mResultTv.setText(idcard);
                        mResultTv.setTextColor(Color.WHITE);
                    }

                    @Override
                    public void onFailure(String errorMsg) {
                        //人脸识别失败,返回失败信息
                        mResultTv.setText(errorMsg);
                        mResultTv.setTextColor(Color.RED);
                    }
                });
            }
        });
    }

}
